package com.cy.pj.sys.web.controller;

import com.cy.pj.common.util.PageUtil;
import com.cy.pj.sys.pojo.SysLog;
import com.cy.pj.sys.service.SysLogService;
import com.cy.pj.sys.web.pojo.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController  // 相当于@Controller + @ResponseBody，其方法都已JSON格式输出，不用再写jackjson配置了
@RequestMapping("/log/") // 请求URL：http://localhost/sysLog/  默认端口80，不用写
public class SysLogController {
    private final SysLogService SysLogService;

    public SysLogController(SysLogService SysLogService) {
        this.SysLogService = SysLogService;
    }

    @GetMapping({"id"})
    public JsonResult doFindById(@PathVariable Long id) throws IOException {
        return new JsonResult(SysLogService.findById(id));
    }

    @DeleteMapping("{ids}")
    public JsonResult doDeleteById(@PathVariable Long... ids) {
        return new JsonResult("delete OK",
                SysLogService.deleteById(ids));
    }

    @GetMapping
    public JsonResult doFindLogs(SysLog sysLog) throws IOException {
        PageInfo<SysLog> pageInfo = PageUtil.startPage()
                .doSelectPageInfo(()
                        -> SysLogService.findLogs(sysLog));
        return new JsonResult(pageInfo);
//        return new JsonResult(SysLogService.findLogs(sysLog));
    }
}
